package Year22_Month04.day22;

public class 环形链表two {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        boolean flag = false;
        ListNode tmp = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                flag = true;
                break;
            }
        }
        if(flag == false){
            return null;
        }
        while(slow != tmp){
            slow = slow.next;
            tmp = tmp.next;
        }
        return tmp;
    }
}
